import threading


class FooBar:
    def __init__(self, n):
        self.n = n
        self.fooevt = threading.Event()
        self.barevt = threading.Event()
        self.fooevt.set()

    def foo(self):
        for i in range(self.n):
            self.fooevt.wait()
            print('foo', end="")
            self.fooevt.clear()
            self.barevt.set()

    def bar(self):
        for i in range(self.n):
            self.barevt.wait()
            print('bar', end="")
            self.barevt.clear()
            self.fooevt.set()


foobar = FooBar(20)
threading.Thread(target=foobar.foo, name="foo").start()
threading.Thread(target=foobar.bar, name="bar").start()


# 实现的非常好，简洁明了。可以考虑一下，如果改成线程池，该怎么实现？